package com.zte.claa.inficombo.csa.app.service.redis;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zte.claa.inficombo.csa.app.model.dev.CmdModelBean;
import com.zte.claa.inficombo.csa.app.model.dev.DataModelBean;
import com.zte.claa.inficombo.csa.app.model.dev.DevBase;
import com.zte.claa.inficombo.csa.app.model.dev.DevTpExtData;
import com.zte.claa.inficombo.csa.app.model.dev.DevTypeBase;
import com.zte.claa.inficombo.csa.app.model.dev.ResIndexDataModel;
import com.zte.claa.inficombo.csa.app.model.proj.ProjectDBPara;
import com.zte.claa.inficombo.csa.app.model.redis.AlarmCode;
import com.zte.claa.inficombo.csa.app.model.redis.DevExtData;
import com.zte.claa.inficombo.csa.app.model.redis.DevModelChgData;
import com.zte.claa.inficombo.csa.app.model.redis.ProjectDev;
import com.zte.claa.inficombo.csa.app.util.JsonUtils;

public class WebModelChgListener extends DefaultMsgListener {
	private Logger logger = LoggerFactory.getLogger(WebModelChgListener.class);

	public WebModelChgListener(RedisMsgListenService listenService) {
		super(listenService);
	}

	@Override
	public void processMsgData(String jsonMsg) {
		try{
			JSONObject json = new JSONObject(jsonMsg);
			String valStr = JsonUtils.getJsonAttrVal(json, "dt", "0");
			byte dt = Byte.parseByte(valStr);
			valStr = JsonUtils.getJsonAttrVal(json, "opflag", "0");
			byte opflag = Byte.parseByte(valStr);
			valStr = json.get("data").toString();
			switch(dt){
			case DevModelChgData.DT_CMDMODEL:
				this.processCmdModelChg(valStr, opflag);
				break;
			case DevModelChgData.DT_DATAMODEL:
				this.processDataModelChg(valStr, opflag);
				break;
			case DevModelChgData.DT_DEV:
				this.processDevChg(valStr, opflag);
				break;
			case DevModelChgData.DT_DEVTP_BASE:
				this.processDevTypeChg(valStr, opflag);
				break;
			case DevModelChgData.DT_FMMODEL:
				this.processFmModelChg(valStr, opflag);
				break;
			case DevModelChgData.DT_PRDB_PARA:
				this.processProjDBParaChg(valStr, opflag);
				break;
			case DevModelChgData.DT_DEV_EXTDATA:
				this.processDevExtDataChg(valStr, opflag);
				break;
			case DevModelChgData.DT_DEVTP_EXT:
				this.processDevTpExtChg(valStr, opflag);
				break;
			case DevModelChgData.DT_RES_INDEXMODEL:
				this.processResIndexModelChg(valStr, opflag);
				break;
			default:
				logger.warn("Invalid data type: "+dt);
				break;
			}
			logger.info("Process msg ok: dt="+dt+", msg="+jsonMsg);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
		}
	}
	
	private void processCmdModelChg(String jsonData, byte op) {
		CmdModelBean cmdModel = JsonUtils.readValue(jsonData, CmdModelBean.class);
		this.listenService.processCmdModelChg(cmdModel, op);
	}

	private void processDataModelChg(String jsonData, byte op) {
		DataModelBean dm = JsonUtils.readValue(jsonData, DataModelBean.class);
		this.listenService.processDataModelChg(dm, op);
	}
	
	private void processResIndexModelChg(String jsonData, byte op) {
		ResIndexDataModel dm = JsonUtils.readValue(jsonData, ResIndexDataModel.class);
		this.listenService.processResIndexModelChg(dm, op);
	}

	private void processDevChg(String jsonData, byte op) {
		ProjectDev pdev = JsonUtils.readValue(jsonData, ProjectDev.class);
		DevBase dev = new DevBase();
		dev.setAppEui(pdev.getAppeui());
		dev.setDevEui(pdev.getDeveui());
		dev.setDevName(pdev.getDevname());
		dev.setDevType(pdev.getDevtype());
		dev.setProjectId(pdev.getProjectid());
		dev.setGpsalt(pdev.getGpsalt());
		dev.setGpslat(pdev.getGpslat());
		dev.setGpslng(pdev.getGpslng());
		// 设置devflag，区分lora终端和非lora传感器.
		dev.setDevflag(pdev.getDevflag());
		this.listenService.processDevChg(dev, op);
	}

	private void processDevTypeChg(String jsonData, byte op) {
		DevTypeBase dtp = JsonUtils.readValue(jsonData, DevTypeBase.class);
		this.listenService.processDevTypeChg(dtp, op);
	}

	private void processFmModelChg(String jsonData, byte op) {
		AlarmCode fm = JsonUtils.readValue(jsonData, AlarmCode.class);
		this.listenService.processFmModelChg(fm, op);
	}
	
	/**
	 * 
	 * @param jsonData
	 * @param op
	 */
	private void processProjDBParaChg(String jsonData, byte op) {			
		ProjectDBPara dbpara = JsonUtils.readValue(jsonData, ProjectDBPara.class);
		this.listenService.processProjDBParaChg(dbpara, op);
	}
	
	private void processDevExtDataChg(String jsonData, byte op) {			
		DevExtData devExtData = JsonUtils.readValue(jsonData, DevExtData.class);
		this.listenService.processDevExtDataChg(devExtData, op);
	}
	
	private void processDevTpExtChg(String jsonData, byte op) {
		DevTpExtData dtpExt = JsonUtils.readValue(jsonData, DevTpExtData.class);
		this.listenService.processDevTpExtChg(dtpExt, op);
	}
	
}
